﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using SinBiota.Model.Entities;
using SinBiota.Model.Persistence.Repositories.Interfaces;
using NHibernate;
using NHibernate.Criterion;

namespace SinBiota.Model.Persistence.Repositories.Implementation
{
    public class MunicipalityRepository : RepositoryAbstract<Municipality,int>, IMunicipalityRepository
    {

        public MunicipalityRepository(ISession session) : base(session)
        {
        }

        public IList<Municipality> FindByState(State state)
        {
            // List and return all municipalities that have this state:
            return FindByEqRestrictionListResult(x => x.State, state);
        }

        public Municipality FindByName(string name)
        {
            // List and return all municipalities that have this name:
            return FindByEqRestrictionUniqueResult(x => x.Name, name);
        }

        public IList<Municipality> FindByNameLike(string name, State? state)
        {
            var list = FindByInffix(x => x.Name, name);
            return !state.HasValue ? list : list.Where(m => m.State.Equals(state.Value)).ToList();
        }
    }
}